package com.qf.smartplatform.websocket;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.smartplatform.pojo.SysHumiture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

/*** @author Tgb
 * @version V1.0
 * @Project smartplatform
 * @Package com.qf.smartplatform.websocket
 * @Description:
 * @Date 2022/9/14 21:33
 */
@Component
public class HumitureWebSocketHandler extends MyBaseWebScoketHandler{
    private ObjectMapper objectMapper;
    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }
    @Autowired
    private ApplicationContext context;

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        String payload = message.getPayload();
        String deviceId = (String) session.getAttributes().get("deviceId");
        System.out.println(payload+":::::::::::::"+deviceId);
        SysHumiture sysHumiture = objectMapper.readValue(payload, SysHumiture.class);//解析数据

        sysHumiture.setDeviceId(deviceId);
        sysHumiture.setTemperature((sysHumiture.getTemperature()-32)/1.8);
        context.publishEvent(sysHumiture);


        super.handleTextMessage(session, message);
    }

    @Override
    public boolean isUpdateStatus() {
        return true;
    }

}
